package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.OrderBill;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.query.OrderBillQueryObject;
import cn.wolfcode.wms.service.IOrderBillService;
import cn.wolfcode.wms.service.ISupplierService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("orderBill")
public class OrderBillController {
    @Autowired
    private IOrderBillService service;
    @Autowired
    private ISupplierService supplierService;
    @RequiredPermission("订单列表")
    @RequestMapping("list")
    public String list(Map map, @ModelAttribute("qo") OrderBillQueryObject qo) {
        map.put("suppliers",supplierService.list());
        PageResult pageResult = service.query(qo);
        map.put("pageResult", pageResult);
        return "orderBill/list";
    }
    @RequiredPermission("订单编辑")
    @RequestMapping("input")
    public String input(Map map, Long id) {
        map.put("suppliers",supplierService.list());
        if (id != null) {
            OrderBill orderBill = service.get(id);
            map.put("orderBill", orderBill);
        }
        return "orderBill/input";
    }
    @RequestMapping("view")
    public String view(Map map, Long id) {
        if (id != null) {
            OrderBill orderBill = service.get(id);
            map.put("orderBill", orderBill);
        }
        return "orderBill/view";
    }
    @RequestMapping("audit")
    @ResponseBody
    public JSONResult audit(Long id) {
        try{
            service.audit(id);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return JSONResult.success("审核成功");
    }
    @RequiredPermission("订单保存或更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(OrderBill orderBill) {
        try {
            service.saveOrUpdate(orderBill);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return JSONResult.success("操作成功");
    }
    @RequiredPermission("订单删除")
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        if (id != null) {
            try {
                service.delete(id);
            } catch (Exception e) {
                return JSONResult.mark(e.getMessage());
            }
        }
        return JSONResult.success("删除成功");
    }
}
